#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#

import time

from column import Column
from supercolumn import SuperColumn
from columnfamily import ColumnFamily

from jutebag import phpserialize


COLUMN_FAMILY_SEPARATOR = ':'

class   Row(object) :
    def __init__(self, table_name=None, key=None) :
        self.table_name = table_name
        self.row_key = key

        # map< column_family_name, column_family_object >
        self._modifications = {}
        ## __init__()

    def __del__(self) :
        self._modifications = {}
        ## __del__()

    @staticmethod
    def splitColumnAndColumnFamilyName(column_family_name) :
        return  column_family_name.split( COLUMN_FAMILY_SEPARATOR )
        ## splitColumnAndColumnFamilyName()

    def addColumnFamily(self, column_family) :
        cfname = column_family.name
        self._modifications[ cfname ] = column_family
        ## addColumnFamily()

    def getColumnFamilies(self) :
        return self._modifications.values()
        ## getColumnFamilies()

    def getColumnFamily(self, column_family_name) :
        if column_family_name in self._modifications :
            return self._modifications[ column_family_name ]

        return None
        ## getColumnFamily()

    def clear(self) :
        self._modifications.clear()
        ## clear()

    def __str__(self) :
        return '<Row table:%s key:%s>' % (self.table_name, self.row_key)
        ## __str__()

    def __eq__(self, otherRow) :
        if not otherRow :
            return False
        if not isinstance(otherRow, Row) :
            return False

        if self.table_name != otherRow.table_name : 
            return False
        if self.row_key != otherRow.row_key :
            return False
        for cfname,cf in self._modifications.iteritems() :
            other_cf = otherRow.getColumnFamily(cfname)
            if cf != other_cf :
                return False
        return True
        ## __eq__()

    def getKey(self) :
        return self.row_key
        ## getKey()
    key = property(getKey,)

    ####-----------------------------------------------------------####

    class   Serializer(object) :

        @staticmethod
        def serialize(row) :
            '''serialize row into stream
            '''
            return phpserialize.serialize(row)
            ## serialize()

        @staticmethod
        def deserialize(row_serialized) :
            '''deserialize row
            '''
            o = phpserialize.unserialize(row_serialized)
            return o
            ## deserialize()

        ## class Serializer()

    ## class Row()

